package com.meitianhui.convenitenbuy.module.home.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/11/16.
 */

public class HomeSearchAdapter  extends BaseQuickAdapter<HomeGoodsInfo.Convient,BaseViewHolder>{
    private Context context;
    public HomeSearchAdapter(Context context, @Nullable List<HomeGoodsInfo.Convient> data) {
        super(R.layout.item_home_edinburgh_goods_item, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeGoodsInfo.Convient item) {
        TextView tvType = helper.getView(R.id.tv_type);
        ImageView ivStorePic = helper.getView(R.id.iv_store_pic);
        ImageView ivStoreSellOut = helper.getView(R.id.iv_store_sell_out);
        helper.setText(R.id.tv_hand_price,"¥" +item.getDiscount_price());   //到手的价格
        helper.setText(R.id.tv_title,"\u3000\u3000\u3000"+item.getTitle());  //标题
        String taoBaoPrice=item.getTaobao_price();   //淘宝价格
        if(TextUtils.isEmpty(item.getTaobao_link())) {
            tvType.setBackgroundResource(R.drawable.edinburgh_text_bg);
            tvType.setTextColor(mContext.getResources().getColor(R.color.homeEdinburghText));
            tvType.setText("淘宝");
        }else{
            tvType.setBackgroundResource(R.drawable.edinburgh_text_blue_bg);
            tvType.setTextColor(mContext.getResources().getColor(R.color.speedText));
            tvType.setText("极速领");
        }
        if(TextUtils.isEmpty(taoBaoPrice)) {
            helper.setText(R.id.tv_taobao_price,"淘宝价¥" + item.getMarket_price());
            String money= StringUtils.moneySub(item.getMarket_price(),item.getDiscount_price());
            helper.setText(R.id.tv_save_price,"节省¥"+money);
        }else{
            helper.setText(R.id.tv_taobao_price,"淘宝价¥" +taoBaoPrice);
            String money= StringUtils.moneySub(taoBaoPrice,item.getDiscount_price());
            helper.setText(R.id.tv_save_price,"节省¥"+money);
        }
        try {
            int saleQty=Integer.parseInt(item.getSale_qty());
            int sellCount=Integer.parseInt(item.getStock_qty())-saleQty;
            helper.setText(R.id.iv_sell_count,sellCount+"");   //已售
            if(sellCount<=0) {
                ivStoreSellOut.setVisibility(View.VISIBLE);
            }else{
                ivStoreSellOut.setVisibility(View.GONE);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        GlideUtils.setRectImage(context,item.getPic_info_url(),R.mipmap.icon_default_picture_store,R.mipmap.icon_default_picture_store,ivStorePic);

    }
}
